<template>
    <div class="ele-body">
      <!-- 搜索表单 -->
      <payaccountSearch @search="reload"  />
      <!-- 数据表格 -->
      <el-card shadow="never">
        <ele-pro-table
          ref="table"  row-key="id" :columns="columns" :datasource="datasource"  :need-page="true"  :cell-style="cellStyle"  @done="onDone"  :height="tableHeight" :full-height="tableFullHeight"
        >
          <!-- 表头工具栏 -->
          <template slot="toolbar">
            <!-- <el-button
              size="small"
              type="primary"
              icon="el-icon-plus"
              class="ele-btn-icon"
              @click="openEdit({},'1')"
            >
              添加
            </el-button> -->
            <el-button
            v-permission="'Financial_Payaccountauit_Export'"
              size="small"
              icon="el-icon-upload2"
              class="ele-btn-icon"
              @click="reportexcel"
            >
              导出
            </el-button>
          </template>
           <!-- 标题列 -->
           <template slot="surplusAmountCn" slot-scope="{ row }">
            ￥{{ row.surplusAmountCn }}
          </template>
           <!-- 标题列 -->
           <template slot="auitAmountCn" slot-scope="{ row }">
            ￥{{ row.auitAmountCn }}
          </template>
          <template slot="payAmountCn" slot-scope="{ row }">
            ￥{{ row.payAmountCn }}
          </template>
           <!-- 操作列 -->
          <template slot="orderNo" slot-scope="{ row }"  >
            <el-link  type="primary"   @click="clickbillNo(row)" >{{ row.orderNo }}</el-link>
          </template>
        </ele-pro-table>
      </el-card>
       <!-- 编辑弹窗 -->
       <!-- <payaccountDetail :data="current" :visible.sync="showEdit" @done="reload" :btnIndex="btnIndex"/> -->
      <!-- 导出数据 -->
      <ExportDialog ref="ExportDialog"></ExportDialog>
    </div>
  </template>
  
  <script>
    
    import { removePageTab } from '@/utils/page-tab-util';
    import ExportDialog from '@/components/Excel/ExportDialog'
    // import payaccountDetail from './components/payaccount-detail.vue';
    import payaccountSearch from './components/payaccount-search.vue';
    import { QueryPayaccountauitByCondition } from '@/api/finance/payaccountauit';

    export default {
      name: 'Payaccountauit',
      components: {payaccountSearch,ExportDialog},
      data() {
        return {
          // 表格列配置
          columns: [
            {columnKey: 'index', type: 'index', width: 50, align: 'center', showOverflowTooltip: true, fixed: 'left', label:'序号',},
            {prop: 'orderNo', label: '单据编号', width: 120,showOverflowTooltip: true,slot:'orderNo', sortable: 'orderNo',align: 'center',},
            {prop: 'auitOrderTypeDesc', label: '单据类型',width: 150, showOverflowTooltip: true, align: 'center', sortable: 'auitOrderType'},
            {prop: 'supplierTypeDesc', label: '供应商类型',width: 150, showOverflowTooltip: true, align: 'center', sortable: 'supplierTypeId'},
            {prop: 'supplierDesc', label: '供应商',width: 150, showOverflowTooltip: true, align: 'center', sortable: 'supplierId'},
            {prop: 'currencyDesc', label: '币种',width: 150, showOverflowTooltip: true, align: 'center', sortable: 'currencyId'},          
            {prop: 'payAmount', label: '应付金额',width: 120, showOverflowTooltip: true, align: 'center', sortable: 'payAmount'},
            {prop: 'auitAmount', label: '核销金额',width: 120, showOverflowTooltip: true, align: 'center', sortable: 'auitAmount'},
            {prop: 'surplusAmount', label: '剩余金额',width: 120, showOverflowTooltip: true,sortable:'surplusAmount', align: 'center',},
            {prop: 'rate', label: '汇率',width: 100, showOverflowTooltip: true, align: 'center',},
            {prop: 'payAmountCn', label: '应付金额(￥)',width: 150, showOverflowTooltip: true,sortable:'payAmountCn', align: 'center',},
            {prop: 'auitAmountCn', label: '核销金额(￥)',width: 150,  showOverflowTooltip: true, align: 'center',sortable: 'auitAmountCn',},
            {prop: 'surplusAmountCn', label: '剩余金额(￥)',width: 150, showOverflowTooltip: true, align: 'center', sortable: 'surplusAmountCn',},
            {
              prop: 'createDate', label: '单据日期', width: 120,showOverflowTooltip: true, align: 'center',sortable: 'createDate',
              formatter: (row, column, cellValue) => {return this.$util.toDateString(cellValue,"YYYY-MM-DD");}
            },
          ],
          //是否详情
          btnIndex: '1',
          fixedHeight:true,
          // 当前编辑数据
          current: null,
          // 是否显示编辑弹窗
          showEdit: false,
          // 导出列
          exportList: [
            { name: "单据编号", keys: "orderNo", status: true },
            { name: "单据类型", keys: "auitOrderTypeDesc", status: true },
            { name: "供应商类型", keys: "supplierTypeDesc", status: true },
            { name: "供应商", keys: "supplierDesc", status: true },
            { name: "币种", keys: "currencyDesc", status: true },
            { name: "单据日期", keys: "createDate", status: true },
            { name: "应付金额", keys: "payAmount", status: true },
            { name: "核销金额", keys: "auitAmount", status: true },
            { name: "剩余金额", keys: "surplusAmount", status: true },
            { name: "应付金额(￥)", keys: "payAmountCn", status: true },
            { name: "核销金额(￥)", keys: "auitAmountCn", status: true },
            { name: "剩余金额(￥)", keys: "surplusAmountCn", status: true },
            { name: "汇率", keys: "rate", status: true },
          ]
        };
      },
      computed: {
        // 表格高度
        tableHeight() {
          var fh = this.fixedHeight ? 'calc(100vh - 250px)' : void 0;

          return fh;
        },
        // 表格全屏时高度
        tableFullHeight() {
          var fh =  this.fixedHeight ? 'calc(100vh - 126px)' : null;

          return fh;
        },
      },
      methods: {
        /* 表格数据源 */
        async datasource(params) {
          params.where.pageindex = params.page;
          params.where.pagesize = params.limit;
  
          //添加排序
          if(params.order.order == null || params.order.order == undefined){
            params.where.orderBys = [{ sort: "createDate", order: 1, }];
          }else{
            params.where.orderBys = [];
            params.where.orderBys.push({sort:params.sorter.column.sortable,order:params.order.order=='asc'?0:1});
          }
        
          let data = await QueryPayaccountauitByCondition(params.where);

          this.$nextTick(() => {
            this.$refs.table.doLayout();
          });
          
          return {
              list:data.items,
              count:data.totalrecords,
          };
        },
  
        /* 表格渲染完成回调 */
        onDone({ data }) {
          this.supplierList = data;
        },
        /* 刷新表格 */
        reload(where) {
          
          this.$refs.table.reload({ where: where });
        },
        
        /**
         * 显示编辑
         * @param row 当前行对像
         */
        openEdit(row,index) {
          this.current = row;
          this.btnIndex = index;
          this.showEdit = true;
        },
        /* 删除 */
        remove(row) {
          const loading = this.$loading({ lock: true });
          DeleteBankaccount(row.id)
            .then((msg) => {
              loading.close();
              this.$message.success(msg);
              this.reload();
            })
            .catch((e) => {
              loading.close();
              this.$message.error(e.message);
            });
        },
        /* 导出 */
        reportexcel(pams) {

          //初始化参数
          let params = {
            pagesize: 0,
            pageindex: 1,
            conditions: [],
            orderBys: [{ sort: "createDate", order: 1, }],
          };
          //加载进度条
          const loading = this.$loading({ lock: true });
          //重新请求查询
          this.$refs.table.doRequest(({ where }) => {
          if(where){
            params.orderBys = where.orderBys;
            params.pageindex = where.pageindex;
            params.conditions = where.conditions;
          }
          //查询数据
          QueryPayaccountauitByCondition(params)
            .then((data) => {
              loading.close();
              this.$refs.ExportDialog.showEdits = true
              this.$refs.ExportDialog.checkList = this.exportList
              this.$refs.ExportDialog.titleName = "应付核销账列表"
              this.$refs.ExportDialog.dataList = data.items;
            })
            .catch((e) => {
              loading.close();
              this.$message.error(e.message);
            });
          });
        },

        //单击单号
      clickbillNo(row){
        // 采购订单
        if(row.auitOrderType == 0){
          removePageTab({ key: '/businesshandling/purordermanager' });
          // this.$router.push({path: '/businesshandling/purordermanager', query: { "billNo": row.orderNo } });
          this.$nextTick(() => {
            this.$router.push({path: '/businesshandling/purordermanager', query: { "billNo": row.orderNo } });
          });
        }

        // 入库单
        if(row.auitOrderType == 1){

          // reloadPageTab({ fullPath: '/invoicingmanager/instockmanager' });
          removePageTab({ key: '/invoicingmanager/instockmanager' });
          this.$nextTick(() => {
            this.$router.push({path: '/invoicingmanager/instockmanager', query: { "billNo": row.orderNo } });
          });
          
        }
      },

        // /* 合计 */
        // getSummaries(param){
        //   const sums = [];
        //   sums[0] = '合计' ;
        //   const { columns, data } = param;
        //   columns.forEach((column, index) => {
        //     if(index ==3){
        //       sums[index] =  Number(this.addAmountTotal).toFixed(2);
        //     }else if(index ==4){
        //       sums[index] =  Number(this.reduceAmountTotal).toFixed(2);
        //     }else if(index ==5){
        //       sums[index] =  Number(this.actualAmountTotal).toFixed(2);
        //     }else if(index ==6){
        //       sums[index] = '￥'+Number(this.addAmountTotalCn).toFixed(2);
        //     }else if(index ==7){
        //       sums[index] = '￥'+Number(this.reduceAmountTotalCn).toFixed(2);
        //     }else if(index ==8){
        //       sums[index] = '￥'+Number(this.actualAmountTotal).toFixed(2);
        //     }
        //   });

        //   this.$nextTick(() => {
        //     this.$refs.table.doLayout();
        //   });
        //   return sums;
        // },

        /* 样式 */
        cellStyle(param){
          const { row, column, rowIndex, columnIndex } = param;
          if(columnIndex==6||columnIndex==7||columnIndex==8||columnIndex==9||columnIndex==10||columnIndex==11||columnIndex==12){
            return{
              color:''
            }
          }
        },
      }
    };
  </script>
   <style lang="scss" scoped>
    /*总计字体颜色
    ::v-deep .el-table__footer-wrapper tbody td {
      color: var(--color-primary);
      cursor: pointer;
    }*/
  </style>